package org.jeecg.modules.demo.publicplace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.jeecg.modules.demo.base.entity.ZnzfEmployee;
import org.jeecg.modules.demo.base.mapper.ZnzfEmployeeMapper;
import org.jeecg.modules.demo.publicplace.entity.*;
import org.jeecg.modules.demo.publicplace.mapper.ZnzfPublicApplyMapper;
import org.jeecg.modules.demo.publicplace.mapper.ZnzfPublicMapper;
import org.jeecg.modules.demo.publicplace.mapper.ZnzfPublicOpenMapper;
import org.jeecg.modules.demo.publicplace.service.IZnzfPublicApplyService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @Description: 公共场所预约
 * @Author: jeecg-boot
 * @Date:   2022-04-11
 * @Version: V1.0
 */
@Service
@Transactional
public class ZnzfPublicApplyServiceImpl extends ServiceImpl<ZnzfPublicApplyMapper, ZnzfPublicApply> implements IZnzfPublicApplyService {

    @Resource
    private ZnzfPublicApplyMapper znzfPublicApplyMapper;

    @Resource
    private ZnzfPublicMapper znzfPublicMapper;

    @Resource
    private ZnzfPublicOpenMapper znzfPublicOpenMapper;

    @Resource
    private ZnzfEmployeeMapper znzfEmployeeMapper;

    /**
     * 根据id查询预约表项
     * @param id
     * @return
     */
    @Override
    public ZnzfPublicApply queryPublicApplyById(String id) {
        return znzfPublicApplyMapper.selectById(id);
    }

    /**
     * 根据id查询eid
     * @param id
     * @return
     */
    @Override
    public String queryEidById(String id) {
        return znzfPublicApplyMapper.selectById(id).getEid();
    }

    /**
     * 根据预约表id取消预约
     * @param publicApplyId
     * @return
     */
    @Override
    public int cancelReservation(String publicApplyId) {
        ZnzfPublicApply znzfPublicApply = znzfPublicApplyMapper.selectById(publicApplyId);
        if (znzfPublicApply == null) {
            System.out.println("根据预约表id未找到对应预约表项");
            return 1;
        }
        //修改状态为已取消（3）
        znzfPublicApply.setApplyState(3);
        //更新预约表项
        if (znzfPublicApplyMapper.updateById(znzfPublicApply) == 0) {
            System.out.println("更新预约表项失败");
            return 2;
        }

        String publicOpenId = znzfPublicApply.getPublicOpenId();
        ZnzfPublicOpen znzfPublicOpen = znzfPublicOpenMapper.selectById(publicOpenId);
        if (znzfPublicOpen == null) {
            System.out.println("根据开放时间段表id未找到对应开放时间段表项");
            return 1;
        }
        //修改开放时间段表的当前预约人数 = 原当前预约人数 - 预约表的预约人数
        znzfPublicOpen.setCurrApply(znzfPublicOpen.getCurrApply() - znzfPublicApply.getApplyNum());
        //更新开放时间段表
        if (znzfPublicOpenMapper.updateById(znzfPublicOpen) == 0) {
            System.out.println("更新开放时间段表项失败");
            return 2;
        }

        return 0;
    }

    /**
     * 通过userId查询我的预约信息
     * @param userId
     * @return
     */
    @Override
    public List<ZnzfMyPublicApplyAndStateParam> getByUserId(String userId) {
        LambdaQueryWrapper<ZnzfEmployee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfEmployee::getUserId, userId);
        ZnzfEmployee znzfEmployee = znzfEmployeeMapper.selectOne(queryWrapper);
        if (znzfEmployee == null)
            return null;
        //获取职工id
        String employeeId = znzfEmployee.getId();

        //创建我的预约和状态的列表
        List<ZnzfMyPublicApplyAndStateParam> list = new ArrayList<>();

        //根据预约状态分别查询历史预约情况
        for (int i = 0; i < 4; i++) {
            //创建我的预约和状态的对象
            ZnzfMyPublicApplyAndStateParam znzfMyPublicApplyAndStateParam = new ZnzfMyPublicApplyAndStateParam();
            //创建我的预约列表
            List<ZnzfMyPublicApplyParam> znzfMyPublicApplyParamList = new ArrayList<>();
            LambdaQueryWrapper<ZnzfPublicApply> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1
                    .eq(ZnzfPublicApply::getEid, employeeId)
                    .eq(ZnzfPublicApply::getApplyState, i);
            List<ZnzfPublicApply> znzfPublicApplyList = znzfPublicApplyMapper.selectList(queryWrapper1);

            for (int j = 0; j < znzfPublicApplyList.size(); j++) {
                //创建我的预约对象
                ZnzfMyPublicApplyParam znzfMyPublicApplyParam = new ZnzfMyPublicApplyParam();

                ZnzfPublic znzfPublic = znzfPublicMapper.selectById(znzfPublicApplyList.get(j).getPublicId());
                if (znzfPublic != null) {
                    //设置预约场所
                    znzfMyPublicApplyParam.setName(znzfPublic.getName());
                    //设置地址
                    znzfMyPublicApplyParam.setAddress(znzfPublic.getAddress());
                }
                ZnzfPublicOpen znzfPublicOpen = znzfPublicOpenMapper.selectById(znzfPublicApplyList.get(j).getPublicOpenId());
                if (znzfPublicOpen != null) {
                    //设置预约的开始时间
                    znzfMyPublicApplyParam.setStart(znzfPublicOpen.getStart());
                    //设置预约的结束时间
                    znzfMyPublicApplyParam.setEnd(znzfPublicOpen.getEnd());
                }
                //设置预约项的主键id
                znzfMyPublicApplyParam.setId(znzfPublicApplyList.get(j).getId());
                //设置申请预约的人数
                znzfMyPublicApplyParam.setApplyNum(znzfPublicApplyList.get(j).getApplyNum());
                //设置申请预约的时间
                znzfMyPublicApplyParam.setCreateTime(znzfPublicApplyList.get(j).getCreateTime());

                znzfMyPublicApplyParamList.add(znzfMyPublicApplyParam);
            }
            znzfMyPublicApplyAndStateParam.setZnzfMyPublicApplyParamList(znzfMyPublicApplyParamList);

            znzfMyPublicApplyAndStateParam.setApply_state(i);
            list.add(znzfMyPublicApplyAndStateParam);
        }

        return list;
    }

    /**
     * 根据eid查询用户所属项目下所有公共场所类型
     * @param eid
     * @return
     */
    @Override
    public List<Integer> queryAllPublicTypeByEid(String eid) {
        List<Integer> publicTypeList = new LinkedList<>();
        LambdaQueryWrapper<ZnzfPublicApply> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfPublicApply::getEid, eid);
        List<ZnzfPublicApply> znzfPublicApplyList = znzfPublicApplyMapper.selectList(queryWrapper);
        for (int i = 0; i < znzfPublicApplyList.size(); i++) {
            ZnzfPublicApply znzfPublicApply = znzfPublicApplyList.get(i);
            String publicId = znzfPublicApply.getPublicId();
            // 根据公共场所id查找到一条公共场所记录
            LambdaQueryWrapper<ZnzfPublic> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(ZnzfPublic::getId, publicId);
            ZnzfPublic znzfPublic = znzfPublicMapper.selectOne(queryWrapper1);
            // 判断对应的id是否为空
            if (znzfPublic != null) {
                publicTypeList.add(znzfPublic.getType());
            }
        }
        return publicTypeList;
    }

    /**
     * 插入一条预约记录
     * @param publicId
     * @param publicOpenId
     * @param eid
     * @param applyNumber
     * @return
     */
    @Override
    public void insertPublicApply(String publicId, String publicOpenId, String eid, int applyNumber) {
        ZnzfPublicApply znzfPublicApply = new ZnzfPublicApply();
        znzfPublicApply.setPublicId(publicId);
        znzfPublicApply.setPublicOpenId(publicOpenId);
        znzfPublicApply.setEid(eid);
        znzfPublicApply.setApplyNum(applyNumber);
        znzfPublicApply.setApplyState(0);
        znzfPublicApplyMapper.insert(znzfPublicApply);
        // 更新预约时间段表
        LambdaQueryWrapper<ZnzfPublicOpen> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ZnzfPublicOpen::getId, publicOpenId);
        ZnzfPublicOpen znzfPublicOpen = znzfPublicOpenMapper.selectOne(queryWrapper);
        if (znzfPublicOpen != null) {
            znzfPublicOpen.setCurrApply(znzfPublicOpen.getCurrApply()+applyNumber);
        }
        znzfPublicOpenMapper.updateById(znzfPublicOpen);
    }
}
